banner = {}

banner.pageIndex = 0;
banner.pageSize = 50;

banner.level = 0;
banner.category;
banner.targetId = 0;

banner.list = function(){
    main.init();
    banner.pageIndex = 0; 
    $("#content").html(fly.template('http://naima.vn/banner/list.html',{
        
    }));   
    banner.load(); 
}

banner.search = function(){ 
    banner.pageIndex = 0;  
    banner.load();
}

banner.page = function(page){
    banner.pageIndex = page;
    banner.load();
}

banner.load = function(){   
    fly.ajax({
        service: '/banner/list',
        data: {
            pageIndex: banner.pageIndex,
            pageSize: banner.pageSize,
            type: $('#txt-search-type').val(),
            name: $('#txt-search-name').val(),
            status: $('#txt-search-status').val() 
        },
        success: function(result){
            if(result.status){                            
                //Hiển thị danh sách  
                $("#tbl-list").html(fly.template('http://naima.vn/banner/sub.html',{
                    data:result.data.data  
                }));    
                
                //Hiển thị link phân trang
                $('#paging').html(fly.template('http://naima.vn/component/paging.html', {
                    data:result.data,
                    action: 'banner'
                }));

                //Hiển thị tổng số bản ghi
                $('#lbl-datacount').text(result.data.itemCount);
                
                //Link thay đổi số bản ghi trên trang
                $('#sl-page-size li[num]').removeClass('totalviewactive');
                $('#sl-page-size li[num='+result.data.pageSize+']').addClass('totalviewactive');
                
                $('#sl-page-size li[num]').click(function(){
                    banner.pageIndex = 0;
                    banner.pageSize = $(this).attr('num');
                    banner.load();
                }); 
                
                $("#list-checkall").click(function(){
                    $('input[type=checkbox][for]').attr('checked', $('#list-checkall').is(':checked'))
                });
                
            } else {
                popup.msg(result.message)
            }
        }
    });
    
}

banner.create = function(){
    popup.open('popup-banner-form', 'Thêm banner mới', fly.template('http://naima.vn/banner/form.html',{
        data: null
    }),[
    {
        title: 'Thêm mới',
        fn:function(){
            fly.submitWithFile({
                id: 'banner-form',
                service: '/banner/create',
                success: function(result){
                    popup.msg(result.message);
                    banner.load();
                    popup.close('popup-banner-form');
                }
            });
        }
    },
    {
        title: 'Hủy bỏ',
        fn:function(){
            popup.close('popup-banner-form');
        }
    },
    ]);
    banner.checkType();
    $('select[name="type"], .banner-type').change(function(){
        banner.checkType();
    });
}
banner.checkType = function(){
    if($('.banner-type').val() == 1 || $('.banner-type').val() == 7 || $('.banner-type').val() == 8){
        $('.type').show();
        $('.banner-targetId').hide();
        if($('select[name="type"]').val() == 1){
            $('.banner-image').show();
            $('.banner-content').hide();
            $('.banner-content textarea').val("");
        }
        else{
            $('.banner-image').hide();
            $('.banner-image textarea').val("");
            $('.banner-content').show();
        }
    }
    else{
        $('.type').hide();
        $('.banner-targetId').show();
        $('.banner-image').show();
        $('.banner-content').hide();
        $('.banner-content textarea').val("");
        switch(parseInt($('.banner-type').val())){
            case 2:
                $('.banner-targetId').hide();
                break;
            case 3:
                fly.ajax({
                    service: '/brand/list/',
                    success: function(data){
                        if(data.status){
                            $('.targetId').html("");
                            $.each(data.data, function(){
                                var brand = this;
                                $('.targetId').append('<option value="'+brand.id+'">'+brand.name+'</option>');
                            });                            
                            $('.targetId').val(banner.targetId);
                        }                
                        else{
                            popup.msg(data.message);
                        }
                    }
                });
                break;
            case 4:
                fly.ajax({
                    service: '/seller/list/',
                    success: function(data){
                        if(data.status){
                            $('.targetId').html("");
                            $.each(data.data, function(){
                                var seller = this;
                                $('.targetId').append('<option value="'+seller.id+'">'+seller.name+'</option>');
                            });                            
                            $('.targetId').val(banner.targetId);
                        }         
                        else
                            popup.msg(data.message);
                    }
                });
                break;
            case 5:
                fly.ajax({
                    service: '/category/list',
                    success: function(data){
                        if(data.status){
                            $('.targetId').html("");
                            $.each(data.data, function(){
                                var cat = this;
                                if(cat.parentId == 0)
                                    $('.targetId').append('<option value="'+cat.id+'">'+cat.name+'</option>');
                            });
                            $('.targetId').val(banner.targetId);                            
                        }
                        else
                            popup.msg(data.message);
                    }
                });
                break;
            case 6:
                $('.banner-targetId').hide();
                break;
        }
    }
};
banner.printCat = function(parentId){
    $.each(banner.category, function(){
        var cat = this;
        if(cat.parentId == 0){
            banner.level ++;
            var tmp = "";
            var i = banner.level;
            while(i){
                tmp += "- - ";
                i--;
            }
            $('.targetId').append('<option value="'+cat.id+'">'+tmp+cat.name+'</option>');
            banner.printCat(cat.id);
            banner.level --;
        }
    });
}
banner.edit = function(id){
    fly.ajax({
        service: '/banner/get/',
        data: {
            id: id
        },
        success: function(result){
            banner.targetId = result.data.targetId;
            popup.open('popup-banner-form', 'Sửa banner', fly.template('http://naima.vn/banner/form.html',{
                data:result.data
            }),[
            {
                title: 'Sửa',
                fn:function(){
                    fly.submitWithFile({
                        id: 'banner-form',
                        service: '/banner/edit',
                        success: function(rs){
                            popup.msg(rs.message);
                            banner.load();
                            popup.close('popup-banner-form');
                        }
                    });
                }
            },
            {
                title: 'Hủy bỏ',
                fn:function(){
                    popup.close('popup-banner-form');
                }
            }
            ]);      
            $('.banner-type').val(result.data.type);
            banner.checkType();
            $('select[name="type"], .banner-type').change(function(){
                banner.checkType();
            });        
        }
        
    });
    
}

banner.del = function(id){
    popup.confirm("Bạn có chắc muốn xóa banner này ?", function(){
        fly.ajax({
            service: '/banner/delete/',
            data: {
                id: id
            },
            success: function(result){
                popup.msg(result.message);
                banner.load();
                popup.close('popup-banner-form');
            }
        });
    });
    
}

banner.changeStatus = function(id){
    fly.ajax({
        service: '/banner/changeStatus/',
        data: {
            id: id
        },
        success: function(data){
            if(data.status){
                if(data.data == 1)
                    $('.published-'+id).attr('src',fly.assetsUrl + '/images/icon-enable.png');
                else
                    $('.published-'+id).attr('src',fly.assetsUrl + '/images/icon-disable.png');
            }
            else
                popup.msg(data.message);
        }
    });
}

banner.getBrand = function(){
    if(!banner.brand){
        fly.ajax({
            service:'/brand/list',
            async: false,
            success: function(result){
                banner.brand = result.data;
            }
        });
    }
    return banner.brand;
}

banner.getSeller = function(){
    if(!banner.seller){
        fly.ajax({
            service:'/seller/list',
            async: false,
            success: function(result){
                banner.seller = result.data;
            }
        });
    }
    return banner.seller;
}

banner.getCat = function(){
    if(!banner.cat){
        fly.ajax({
            service:'/category/list',
            async: false,
            success: function(result){
                banner.cat = result.data;
            }
        });
    }
    return banner.cat;
}

banner.getName = function(type, id){
    var name = '';
    if(type != null && id != null){
        switch(parseInt(type)){
            case 3:
                var brand = banner.getBrand();
                $.each(brand, function(){
                    if(this.id == id){
                        name = this.name;
                    }
                });
                break;
            case 4:
                var seller = banner.getSeller();
                $.each(seller, function(){
                    if(this.id == id){
                        name = this.name;
                    }
                });
                break;
            case 5:
                var cat = banner.getCat();
                $.each(cat, function(){
                    if(this.id == id){
                        name = this.name;
                    }
                });
            default:
                return '';
        }
        return name;
    }
}

banner.saveChange = function(){    
    var data = Array();    
    var i = 0;

    $('input[type=text][for]').each(function(){
        var banner = new Object();  
        
        banner.id = parseInt($(this).attr('for'));  
        banner.position = parseInt($('input[type="text"][for="'+$(this).attr('for')+'"]').attr('value'));
        data[i] = banner;
        i++;
    });
    
    popup.confirm("Bạn có chắc chắn muốn lưu các thay đổi ?", function(){
        fly.ajax({
            service: '/banner/saveChange',
            method: 'post',
            data:{
                banners:JSON.stringify(data)
            },
            success: function(result){
                popup.msg('Cập nhật thay đổi thành công !');                 
            }
        });
    });

}  

banner.delAll = function(){
    var data = Array();
    var i = 0;
    $('input[type=checkbox][for]').each(function(){
        if($(this).is(':checked')){
            data[i] = parseInt($(this).attr('for'));
            i++;
        }
    });
    
    if(data.length <= 0){
        popup.msg('Bạn chưa chọn banner để xóa !');
        return;
    }
    
    popup.confirm("Bạn có chắc chắn xóa những banner này ?", function(){
        fly.ajax({
            service : '/banner/deleteAll',
            data: {ids: JSON.stringify(data)},
            success: function(result){
                popup.msg(result.message);
                for(var i=0; i<result.data.length;i++){
                    $('.table tr[for='+result.data[i]+']').remove();
                }
            }
        });
    });
}